package com.tuanzi.loan.web.bean.settlement;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.entity.settlement.CwOutTicketInfo;
import com.tuanzi.loan.business.entity.settlement.JfSltBackData;
import com.tuanzi.loan.business.entity.settlement.SwMoneyFlow;
import com.tuanzi.loan.business.service.family.ProductInfoService;
import com.tuanzi.loan.business.service.settlement.CompanyInfoService;
import com.tuanzi.loan.business.service.settlement.CwOutTicketService;
import com.tuanzi.loan.business.service.settlement.JfBackDataService;
import com.tuanzi.loan.business.service.settlement.SwMoneyFlowService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.SystemException;
import com.tuanzi.loan.web.configuration.JSFWorkflowBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * 财务收款
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CwBackMoneyBean extends JSFWorkflowBean<SwMoneyFlow> {

    @Autowired
    private JfBackDataService jfBackDataService;
    @Autowired
    private CwOutTicketService cwOutTicketService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private SwMoneyFlowService swMoneyFlowService;
    @Autowired
    private ProductInfoService productInfoService;

    private List<JfSltBackData> jfSltBackDataList;
    private CompanyInfo companyInfo;
    private CwOutTicketInfo cwOutTicketInfo;
    private Long companyId;
    private List<String> productNames;

    @PostConstruct
    public void init(){
        String businessKey = task.getBusinessKey();
        String[] split = businessKey.split(":");
        companyId=Long.valueOf(split[1]);
        $=swMoneyFlowService.getLatestSwMoneyFlowByCompanyId(companyId);
        jfSltBackDataList = jfBackDataService.findJfSltBackDataListByProcInstId(task.getProcInstId());
        companyInfo=companyInfoService.findOne(Long.valueOf(companyId));
        cwOutTicketInfo=cwOutTicketService.getLatestOutTicketInfoByCompanyId(companyId);
        List<ProductInfo> productInfoList = productInfoService.listAllProductInfoByCompanyId(companyId);
        productNames = productInfoList.parallelStream().map(pro->{
            return pro.getName();
        }).collect(Collectors.toList());
    }

    public void saveSwMoneyFlowInfo(){
        swMoneyFlowService.saveOrUpdate($);
        addSuccessfullyMessage();
    }


    public void fileUpload(FileUploadEvent event){
        $.setPingzhengUrl(this.uploadImg(event.getFile()));
        FacesMessage message = new FacesMessage("上传成功！", event.getFile().getFileName() + " 上传成功。");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(SETTLEMENT_CYCLE, APPROVAL_TYPE, COOPERATION_MODE, COMPANY_BACKGROUND, SETTLEMENT_BILLING_CONTENT,SETTLEMENT_SPREAD_CHANNEL);
    }

    @Override
    public void onSave() {
        saveSwMoneyFlowInfo();
    }

    @Override
    public void validate() {
        if(StringUtils.isBlank($.getActualDate().toString())&&StringUtils.isBlank($.getActualMoney().toString())){
            throw new SystemException("实收信息不能为空");
        }
    }
}
